<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;
use think\Request;

class Login extends Controller
{
    public function login()
    {
        return view();
    }

    public function dologin()
    {
        // 接收表单的数据
        $res = input('post.');
//        dump($res);die;

        // 去user表验证数据是否存在
        $list = db('user')
            ->where([
                'username' => $res['username'],
                'userpass' => md5($res['userpass'])
            ])
            ->find();
//        dump($list);die;
        if (!empty($list)) {
            // 判断用户是否激活
            if ($list['status'] != 1) {
                $this->error('您的账号已冻结');
            }

            session(null);
            $username = $list['username'];
            $userid = $list['id'];
            $face = $list['face'];
//            dump($face);die;

            $userinfo = Db::view('node', ['id' => 'nodeid', 'name', 'mname', 'aname', 'status' => 'nodestatus'])
                ->view('role_node', ['nid'], 'node.id = role_node.nid')
                ->view('role', ['id'], 'role_node.rid = role.id')
                ->view('user_role', ['rid'], 'role.id = user_role.rid')
                ->view('user', ['id'], 'user_role.uid = user.id')
                ->where([
                    'user.id' => ['=', $list['id']],
                    'user.status&role.status&node.status' => ['=', 1]
                ])
                ->select();
//            dump($userinfo);die;

            session('userid', $userid);
            session('username', $username);
            session('face', $face);
//            dump($face);die;

            $nodename = [];
            // 存放 控制器
            $mname = array();
            // 存放 方法
            $aname = array();

            foreach ($userinfo as $key => $v) {
                $nodename[] = $v['mname'] . '-' . $v['aname'];
            }


            session('node', $nodename);
//            dump($nodename);die;


            $this->success('登录成功(*^▽^*)', 'admin/Index/index');
        } else {
            $this->error('您的账号或者密码不正确', 'admin/Login/login');
        }

    }

    public function layout()
    {
        Session::delete('userid');
        Session::delete('username');
        Session::delete('face');
        $this->redirect('admin/Login/login');
    }
}
